package services

import (
	"encoding/json"

	"cashew.com/cashew_common/configs"
	"github.com/beego/beego/v2/core/logs"
)

type RespBody struct {
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
	Code int         `json:"code"`
}

func ChargingStart(guid, anchorId, viewerId, anchorSubRegionId, viewSubRegionId string) (respData *RespBody, err error) {
	postUrl := configs.ChargingStartUrl
	headers := map[string]string{
		"Msg-Type": "application/json",
	}

	// 构建完整的请求URL，将查询参数添加到URL中
	reqBody := map[string]interface{}{
		"id":           guid,
		"anchor_id":    anchorId,
		"viewer_id":    viewerId,
		"anchor_regin": anchorSubRegionId,
		"viewer_regin": viewSubRegionId,
	}

	respBody, err := HttpCall("POST", postUrl, headers, reqBody)
	if err != nil {
		return nil, err
	}
	logs.Info("ChargingStart HttpCall response :%s", string(respBody))

	respData = &RespBody{}
	err = json.Unmarshal(respBody, &respData)
	if err != nil {
		return nil, err
	}

	return respData, nil
}

func ChargingStop(guid, anchorId, viewerId string) (respData *RespBody, err error) {
	postUrl := configs.ChargingStopUrl
	headers := map[string]string{
		"Msg-Type": "application/json",
	}

	// 构建完整的请求URL，将查询参数添加到URL中
	reqBody := map[string]interface{}{
		"id":        guid,
		"anchor_id": anchorId,
		"viewer_id": viewerId,
	}

	respBody, err := HttpCall("POST", postUrl, headers, reqBody)
	if err != nil {
		return nil, err
	}
	logs.Debug("响应体数据:%s", string(respBody))

	respData = &RespBody{}
	err = json.Unmarshal(respBody, &respData)
	if err != nil {
		return nil, err
	}

	return respData, nil
}
